package File;

import java.io.*;
import java.util.Scanner;

public class Demo20 {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        System.out.println("请输入要搜索的路径：");
        String filrPath=in.next();
        File file=new File(filrPath);
        if (!file.isDirectory()){
            System.out.println("输入的不是目录或目录不存在");
            return;
        }
        System.out.println("请输入文件名：");
        String keyword=in.next();
        srcFile(file,keyword);
    }

    private static void srcFile(File file, String keyword) {
  File[]list=file.listFiles();
    if(list==null){
        return;
    }
        for (File file1:list) {
            if(file1.isFile()){
                dealFile(file1,keyword);
            }else {
srcFile(file1,keyword);
            }

        }
    }

    private static void dealFile(File file1, String keyword) {
if(file1.getName().contains(keyword)){
    System.out.println("文件名包含关键字："+file1.getAbsolutePath());
    return;
}
StringBuilder stringBuilder=new StringBuilder();
try(Reader reader=new FileReader(file1)) {
    while(true){
        char[]chars=new char[1024];
        int n= reader.read(chars);
        if(n==-1)break;
            stringBuilder.append(chars,0,n);

    }
} catch (IOException e) {
    throw new RuntimeException(e);
}

if(stringBuilder.indexOf(keyword)>=0){
    System.out.println("文件内容包含关键字："+file1.getAbsolutePath());
}


    }


}
